package com.tanhua.sso.controller;

import com.tanhua.sso.service.UserInfoService;
import com.tanhua.sso.vo.ErrorResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

@RestController
@RequestMapping("user")
@Slf4j
public class UserInfoController {

    private final UserInfoService userInfoService;

    @Autowired
    public UserInfoController(UserInfoService userInfoService) {
        this.userInfoService = userInfoService;
    }


    /**
     * 完善个人信息-上传头像
     * @param file
     * @return
     */
    @PostMapping("loginReginfo/head")
    public ResponseEntity<Object> loginReginfo(@RequestParam("headPhoto")MultipartFile file, @RequestHeader("Authorization")String token){
        try {
            boolean bool = this.userInfoService.saveUserLogo(file, token);
            if(bool){
                return ResponseEntity.ok(null);
            }
        } catch (Exception e) {
            log.error("您的程序在上传头像时出现一个异常", e);
        }
        ErrorResult errorResult = ErrorResult.builder().errCode("000001").errMessage("保存用户Logo失败！").build();
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorResult);

    }


    /**
     * 完善个人信息-基本信息
     * @param param
     * @param token
     * @return
     */
    @PostMapping("loginReginfo")
    public ResponseEntity<Object> loginReginfo(@RequestBody Map<String,String> param, @RequestHeader("Authorization")String token){
        try {
            boolean bool = this.userInfoService.saveUserInfo(param, token);
            if(bool){
                return ResponseEntity.ok(null);
            }
        } catch (Exception e) {
            log.error("您的程序在完善个人基本信息时出现一个异常", e);
        }
        ErrorResult errorResult = ErrorResult.builder().errCode("000001").errMessage("保存用户信息失败！").build();
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorResult);

    }
}
